<?php
$dbx = OpenDB();
// Die Taskliste ausgeben
$zeit = time() + 7 * 86400;
$queryx = "select ca_prnum, ca_date, ca_title, ca_status, ca_num, ca_end,";
$queryx .= "mi_nname, mi_vname, ca_priority from calendar ";
$queryx .= "left join mitarbeiter on mi_num = ca_minum ";
$queryx .= "where ca_date <= $zeit and ca_status > 0 and ";
$queryx .= "(ca_minum = $unum or ca_minum is null or ca_minum < 1 ";
$queryx .= "or ca_writer = $unum) order by ca_priority desc, ca_end asc";

if (!($resultx = QueryDB($dbx, $queryx))) {
   closeDB($dbx);
   exit ();
}

$nr = numrowsDB($resultx);

if ($nr > 0) {
   $old_prnum = 0;
   $old_date = 0;
   $izaehl = 0;
   $opFirst = 0;
   $odFirst = 0;
   $j = 0;
   echo "<table class=\"sel\" width=\"70%\">\n";
   echo "<tr><th colspan=5 class=\"sel\">";
   Output($dbx, 267, "Zu erledigende Aufgaben und allgemeine Mitteilungen");
   echo "</th></tr>\n";
   echo "<tr><th class=\"sel\" align=\"center\" valign=\"top\" width=\"15%\">";
   Output($dbx, 268, "Datum");
   echo "</th>";
   echo "<th class=\"sel\" align=\"center\" width=\"15%\">";
   Output($dbx, 269, "Zu erl. bis");
   echo "</th>";
   echo "<th class=\"sel\" align=\"center\" valign=\"top\" width=\"5%\">";
   Output($dbx, 270, "Prio.");
   echo "</th>";
   echo "<th class=\"sel\" align=\"center\" valign=\"top\" width=\"25%\">";
   Output($dbx, 271, "f&uuml;r Mitarbeiter");
   echo "</th>";
   echo "<th class=\"sel\" align=\"center\" valign=\"top\" width=\"40%\">";
   Output($dbx, 272, "[Pr.Nr.] offene Aufgaben / Titel");
   echo "</th></tr>\n";

   while ($izaehl < $nr) {
      $data = fetchDB($resultx, $izaehl);
      $ca_prnum = $data[0];
      $ca_date = $data[1];
      $ca_title = $data[2];
      $ca_status = $data[3];
      $ca_num = $data[4];
      $ca_end = $data[5];
      $mi_vname = $data[6];
      $mi_nname = $data[7];
      $ca_priority = $data[8];

      if ($old_prnum != $ca_prnum || !$opFirst) {
         if ($opFirst) {
	    $a = 0;

	    while ($a < $j) {
	       echo $name[$a] . "<br>\n";
	       $a++;
	    }

	    echo "</td><td class=\"sel\" width=\"40%\">";
	    $a = 0;

	    while ($a < $j) {
	       echo $task[$a] . "<br>\n";
	       $a++;
	    }

	    echo "</td></tr>\n";
	    $j = 0;
	 }

//	 if (isset($ca_prnum) && $ca_prnum > 0)
//	    echo "<tr><td colspan=4 class=\"nwb\">$ca_prnum</td></tr>\n";
//	 else
//	    echo "<tr><td colspan=4 class=\"nwb\">Keine Zuordnung</td></tr>\n";

	 echo "<tr><td class=\"sel\" width=\"15%\" valign=\"top\">" . gmdate("j.m.Y", $ca_date) . "</td>\n";

	 if (isset($ca_end)) {
	    if (($ca_end + 86400) < time())
	       echo "<td class=\"sel\" width=\"15%\" valign=\"top\"><font color=\"a00000\"><b>" . gmdate("j.m.Y", $ca_end) . "</b></font></td>\n";
	    else if ($ca_end < time())
	       echo "<td class=\"sel\" width=\"15%\" valign=\"top\"><b>" . gmdate("j.m.Y", $ca_end) . "</b></td>\n";
	    else
	       echo "<td class=\"sel\" width=\"15%\" valign=\"top\">" . gmdate("j.m.Y", $ca_end) . "</td>\n";
	 }
	 else
	    echo "<td class=\"sel\" width=\"15%\" align=\"center\" valign=\"top\"> - </td>\n";

	 echo "<td width=\"5%\" ";

	 if ($ca_priority == 0)
	    echo "class=\"one\">" . GetMessage($dbx, 273, "Nieder") . "</td>";

	 if ($ca_priority == 1)
	    echo ">" . GetMessage($dbx, 274, "Normal") . "</td>";

	 if ($ca_priority == 2)
	    echo "class=\"five\"><b>" . GetMessage($dbx, 275, "Hoch") . "</b></td>";

	 echo "<td class=\"sel\" width=\"25%\">";
	 $old_date = $ca_date;
	 $old_prnum = $ca_prnum;
	 $opFirst = 1;
	 $odFirst = 1;
      }

      if ($old_date != $ca_date || !$odFirst) {
	 if ($odFirst) {
	    $a = 0;

	    while ($a < $j) {
	       echo $name[$a] . "<br>\n";
	       $a++;
	    }

	    echo "</td><td class=\"sel\" width=\"40%\">";
	    $a = 0;

	    while ($a < $j) {
	       echo $task[$a] . "<br>\n";
	       $a++;
	    }

	    echo "</td></tr>\n";
	    $j = 0;
	 }

	 echo "<tr><td class=\"sel\" width=\"15%\" valign=\"top\">" . gmdate("j.m.Y", $ca_date) . "</td>\n";

	 if (isset($ca_end))
	    if (($ca_end + 86400) < time())
	       echo "<td class=\"sel\" width=\"15%\" valign=\"top\"><font color=\"a00000\"><b>" . gmdate("j.m.Y", $ca_end) . "</b></font></td>\n";
	    else if ($ca_end < time())
	       echo "<td class=\"sel\" width=\"15%\" valign=\"top\"><b>" . gmdate("j.m.Y", $ca_end) . "</b></td>\n";
	    else
	       echo "<td class=\"sel\" width=\"15%\" valign=\"top\">" . gmdate("j.m.Y", $ca_end) . "</td>\n";
	 else
	    echo "<td class=\"sel\" width=\"15%\" align=\"center\" valign=\"top\"> - </td>\n";

	 echo "<td width=\"5%\" ";

	 if ($ca_priority == 0)
	    echo "class=\"one\">" . GetMessage($dbx, 273, "Nieder") . "</td>";

	 if ($ca_priority == 1)
	    echo ">" . GetMessage($dbx, 274, "Normal") . "</td>";

	 if ($ca_priority == 2)
	    echo "class=\"five\"><b>" . GetMessage($dbx, 275, "Hoch") . "</b></td>";

	 echo "<td class=\"sel\" width=\"25%\">";
	 $old_date = $ca_date;
	 $odFirst = 1;
      }

      $year = gmdate("Y", $ca_date);
      $month = gmdate("m", $ca_date);
      $today = gmdate("j", $ca_date);

      if (isset($mi_nname))
         $name[$j] = $mi_nname . " " . $mi_vname;
      else
         $name[$j] = " - ";

      $task[$j] = "[$ca_prnum] <a href=\"#\" onClick=\"javascript:open_window('appt.php?year=$year&month=$month&today=$today&id=$ca_num&header=1&menu=0','Kalender',800,600)\">$ca_title</a>";
      $j++;
      $izaehl++;
   }

   if ($j > 0) {
      $a = 0;

      while ($a < $j) {
	 echo $name[$a] . "<br>\n";
	 $a++;
      }

      echo "</td><td class=\"sel\" width=\"40%\">";
      $a = 0;

      while ($a < $j) {
	 echo $task[$a] . "<br>\n";
	 $a++;
      }

      echo "</td></tr>\n";
   }

   echo "</table>\n";

   if ($rstufe != 1)
      echo "<br><br><br><br>\n";
}

// Einige Statistiken anzeigen
if ($unum > 0) {
   $queryx = "select mi_nname, mi_vname from mitarbeiter where mi_num = $unum";

   if (!($resultx = QueryDB($dbx, $queryx))) {
      closeDB($dbx);
      exit();
   }

   $datax = fetchDB($resultx, 0);
   $mi_nname = $datax[0];
   $mi_vname = $datax[1];
   $queryx = "select distinct on (pr_num) pr_num, pr_name, pr_pl from project, plan, task, allocation where ";
   $queryx .= "pl_prnum = pr_num and ta_plnum = pl_num and al_task = ta_num and ";
   $queryx .= "pl_status in (2,3,4) and al_ressource = $unum order by pr_num";

   if (!($resultx = QueryDB($dbx, $queryx))) {
      closeDB($dbx);
      exit();
   }

   $nrx = numrowsDB($resultx);
} else
   $nrx = 0;

if ($nrx > 0) {
   if ($nr > 0)
      echo "<br>\n";
?>
<table class="sel">
   <tr>
      <th class="sel" colspan=5><? Output($dbx, 276, "Aufw&auml;nde in PT pro Projekt f&uuml;r"); ?> <? echo "$mi_nname $mi_vname"; ?></th>
   </tr>
   <tr>
      <th class="sel"><? Output($dbx, 277, "Projekt"); ?></th>
      <th class="sel"><? Output($dbx, 278, "Plan"); ?></th>
      <th class="sel"><? Output($dbx, 279, "IST"); ?></th>
<?
      if ($ist_erledigung) { ?>
      <th class="sel"><? Output($dbx, 280, "Erl. Zeit"); ?></th>
<?
      } ?>
      <th class="sel"><? Output($dbx, 281, "Erl. Arb."); ?></th>
   </tr>
<?
}
   $x = 0;

   while ($x < $nrx) {
      $datax = fetchDB($resultx, $x);
      $pr_num = $datax[0];
      $pr_name = $datax[1];
      $pr_pl = $datax[2];

      # Sicherstellen, dass das gewaehlte Projekt nicht abgeschlossen ist.
      $queryx = "select count(*) from plan where pl_prnum = $pr_num and pl_status = 6";

      if (!($res = QueryDB($dbx, $queryx))) {
         closeDB($db);
         exit();
      }

      $datax = fetchDB($res, 0);

      if ($datax[0] > 0) {
         $x++;
         continue;
      }

      $queryx = "select pl_num, pl_status from plan ";
      $queryx .= "where pl_prnum = $pr_num and pl_status in (2,3,4) ";
      $queryx .= "order by pl_lfd desc, pl_status desc";

      if (!($resplx = QueryDB($dbx, $queryx))) {
         closeDB($dbx);
         exit();
      }

      if (numrowsDB($resplx) > 0) {
         $datax = fetchDB($resplx, 0);
         $pl_num = $datax[0];
         $pl_status = $datax[1];
      } else {
         $x++;
         continue;
      }

      $queryx = "select sum(al_hours) from task, allocation where ";
      $queryx .= "al_task = ta_num and ta_plnum = $pl_num and al_ressource = $unum";

      if (!($resalx = QueryDB($dbx, $queryx))) {
         closeDB($dbx);
         exit();
      }

      $datax = fetchDB($resalx, 0);
      $al_hours = $datax[0];

      $queryx = "select sum(wd_hours)/$leneinheit from wdone where ";
      $queryx .= "wd_prnum = $pr_num and wd_minum = $unum";

      if (!($reswdx = QueryDB($dbx, $queryx))) {
         closeDB($dbx);
         exit();
      }

      $datax = fetchDB($reswdx, 0);
      $wd_hours = $datax[0];
      echo "<tr><td class=\"sel\">$pr_num $pr_name</td>\n";

      if (($unum == $pr_pl || $rstufe == 1) && $pl_status == 2 && $shortcuts) {
	 echo "<td class=\"selnum\"><a href=\"#\" ";
	 echo "onClick=\"javascript:MoveTo('plan.php','menu=2&headline=9&";
	 echo "editplan=$pl_num')\">";
	 echo FormatNum($al_hours, 3);
	 echo "</a></td>\n";
      } else
	 printf("<td class=\"selnum\">%s</td>\n", FormatNum($al_hours, 3));

      if ($rstufe >= 1 && $rstufe <= 3 && $shortcuts) {
	 echo "<td class=\"selnum\"><a href=\"#\" ";

	 if ($pmlight)
	    echo "onClick=\"javascript:MoveTo('istpm.php','menu=1&mitarbeiter=$unum&project=$pr_num')\">";
	 else
	    echo "onClick=\"javascript:MoveTo('ist.php','menu=1&mitarbeiter=$unum&project=$pr_num')\">";

	 echo FormatNum($wd_hours, 3);
	 echo "</a></td>\n";
      } else
	 printf("<td class=\"selnum\">%s</td>\n", FormatNum($wd_hours, 3));

      if ($al_hours == 0)
	 $proz = 0.0;
      else
	 $proz = $wd_hours / $al_hours * 100.0;

      if ($proz > 100)
	 printf("<td class=\"five\" align=\"right\">%s%%</td>\n", FormatNum($proz, 1));
      else
	 printf("<td class=\"selnum\">%s%%</td>\n", FormatNum($proz, 1));

      # Ermitteln der bisher geleisteten Arbeit (Annaeherung!)
      if ($ist_erledigung) {
	 $queryx = "select sum(ta_complete) from task where ta_plnum = $pl_num";

	 if (!($resx = QueryDB($dbx, $queryx))) {
	    closeDB($dbx);
	    exit();
	 }

	 $datax = fetchDB($resx, 0);
	 $sump = $datax[0];
	 $queryx = "select count(*) from task where ta_plnum = $pl_num";

	 if (!($resx = QueryDB($dbx, $queryx))) {
	    closeDB($dbx);
	    exit();
	 }

	 $datax = fetchDB($resx, 0);
	 $sanz = $datax[0];
	 $proz = $sump / $sanz;
	 printf("<td class=\"selnum\">%s%%</td></tr>\n", FormatNum($proz, 1));
      } else
	 echo "</tr>\n";

      $x++;
   }

if (nrx > 0)
   echo "</table>\n";


closeDB($dbx);
unset($queryx);
unset($resultx);
unset($datax);
?>
